import org.junit.Before;
import org.junit.Test;

import fi.foyt.foursquare.api.FoursquareApi;
import fi.foyt.foursquare.api.FoursquareApiException;
import fi.foyt.foursquare.api.Result;
import fi.foyt.foursquare.api.entities.Checkin;
import fi.foyt.foursquare.api.entities.CompactVenue;
import fi.foyt.foursquare.api.entities.CompleteUser;
import fi.foyt.foursquare.api.entities.CompleteVenue;
import fi.foyt.foursquare.api.entities.VenuesSearchResult;

/**
 * Basic search example
 * 
 * @TODO - more examples please :)
 * @author rmangi
 * 
 */
public class BasicExampleUnit {

	// private final String LL = "48.212992, 16.361099";
	private final String LL = "48.2058914, 16.3934925";
	private final String CLIENT_ID = "L3L3ORJGK23GMG24BJ1CGESDOPWO5Q1N5GN52II4UJ0JDOOO";
	private final String CLIENT_SECRET = "4LC3ZUVEYREW252IAJKRTK4FAESPU154102OWBAAUSWCUG51";
	private FoursquareApi foursquareApi;

	@Before
	public void setup() {
		foursquareApi = new FoursquareApi(CLIENT_ID, CLIENT_SECRET, "https://www.foursquare.com");
		foursquareApi.setVersion("20120609");
	}

	@Test
	public void getUserPlaces() {

		try {
			// foursquareApi.authenticateCode("GGDYU5YF1G0SY0VM1BW5M3T2GYG4KFF5BXQSJVDDYDYXXSPA");
			foursquareApi.setoAuthToken("2LK4ZESABRBMWE23WDULRUVKF0AWBQDCVX2ZNSV2ARCQNTHT");
			Result<CompleteUser> result = foursquareApi.user(null);
			CompleteUser cuser = result.getResult();

			Checkin[] items = cuser.getCheckins().getItems();
			System.out.println(items.length);
			for (Checkin checkIn : items) {
				System.out.println(checkIn.getVenue().getName());
				System.out.println(checkIn.getShout());
				System.out.println(checkIn.getTimeZone());
				if (checkIn.getPhotos().getItems().length > 0) {
					System.out.println(checkIn.getPhotos().getItems()[0].getUrl());
				}
				System.out.println("##########");
			}
		} catch (FoursquareApiException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	// @Test
	public void searchVenues() throws FoursquareApiException {
		// First we need a initialize FoursquareApi.

		// After client has been initialized we can make queries.
		Result<VenuesSearchResult> result = foursquareApi.venuesSearch(LL, null, null, null, null, null, null, null, null, null, null);

		if (result.getMeta().getCode() == 200) {

			// if query was ok we can finally we do something with the data
			for (CompactVenue venue : result.getResult().getVenues()) {
				if (venue.getCategories()[0].getName().equalsIgnoreCase("Meeting Room")) {

					System.out.println(venue.getId());
					System.out.println(venue.getName());
					System.out.println(venue.getUrl());
					System.out.println(venue.getClass());
					System.out.println(venue.getCategories()[0].getName());
					System.out.println("**************");
					// TODO: Do something we the data
				}
			}
		} else {
			// TODO: Proper error handling
			System.out.println("Error occured: ");
			System.out.println("  code: " + result.getMeta().getCode());
			System.out.println("  type: " + result.getMeta().getErrorType());
			System.out.println("  detail: " + result.getMeta().getErrorDetail());
		}
	}

	// @Test
	public void addVenue() {
		final String MEETINGROOMCAT = "4bf58dd8d48988d100941735";

		try {
			// foursquareApi.authenticateCode("WBVLEFOWQNS3CFLG3CWJFPQJL5EECWNLJL0SF5WRQ0V0RTGC");
			foursquareApi.setoAuthToken("2LK4ZESABRBMWE23WDULRUVKF0AWBQDCVX2ZNSV2ARCQNTHT");
			Result<CompleteVenue> result = foursquareApi.venuesAdd("MeetingRoomApple", "Marxergasse 29", "Rasomufskygasse", "vienna", "Austria", "1030",
					"+431504040404", LL, MEETINGROOMCAT);
			CompleteVenue venue = result.getResult();
			System.out.println(venue.getName());
			System.out.println(venue.getId());
			System.out.println(venue.getDescription());
			System.out.println(venue.getTips());
		} catch (FoursquareApiException e) {
			e.printStackTrace();
		}
	}

	// @Test
	public void checkIn() throws FoursquareApiException {
		foursquareApi.setoAuthToken("2LK4ZESABRBMWE23WDULRUVKF0AWBQDCVX2ZNSV2ARCQNTHT");
		Result<Checkin> checkIn = foursquareApi.checkinsAdd("5377ac81498e2298adac1862", "MeetingRoomApple", "shoooot", "public", LL, 1d, 0d, 1d);

		System.out.println(checkIn.getResult().getCreatedAt());

	}
}
